package routes

import (
	"blog-backend/internal/app/controllers"
	"blog-backend/internal/middleware"

	"github.com/gin-gonic/gin"
)

func RegisterAPIRoutes(router *gin.Engine) {
	// 创建API路由组
	api := router.Group("/api")

	// 认证控制器
	authController := controllers.NewAuthController()
	api.POST("/auth/register", authController.Register)
	api.POST("/auth/login", authController.Login)

	// 需要认证的路由
	authGroup := api.Group("")
	authGroup.Use(middleware.AuthMiddleware())
	{
		// 用户相关
		authGroup.GET("/auth/profile", authController.Profile)

		// 文章控制器
		postController := controllers.NewPostController()
		authGroup.POST("/posts", postController.CreatePost)
		authGroup.PUT("/posts/:id", postController.UpdatePost)
		authGroup.DELETE("/posts/:id", postController.DeletePost)

		// 评论控制器
		commentController := controllers.NewCommentController()
		authGroup.DELETE("/comments/:id", commentController.DeleteComment)

		// 分类控制器
		categoryController := controllers.NewCategoryController()
		authGroup.POST("/categories", categoryController.CreateCategory)
		authGroup.DELETE("/categories/:id", categoryController.DeleteCategory)
	}

	// 公开路由
	{
		// 文章控制器
		postController := controllers.NewPostController()
		api.GET("/posts", postController.GetPosts)
		api.GET("/posts/:id", postController.GetPost)
		api.GET("/posts/slug/:slug", postController.GetPostBySlug)

		// 评论控制器
		commentController := controllers.NewCommentController()
		api.GET("/posts/:id/comments", commentController.GetComments)
		api.POST("/posts/:id/comments", commentController.CreateComment)

		// 分类控制器
		categoryController := controllers.NewCategoryController()
		api.GET("/categories", categoryController.GetCategories)
		api.GET("/popularCategories", categoryController.GetPopularCategories)


		// 标签控制器
		tagController := controllers.NewTagController()
		api.GET("/tags", tagController.GetTags)
	}

	// Swagger文档路由
	registerSwaggerRoutes(router)
}

// 注册Swagger路由
func registerSwaggerRoutes(router *gin.Engine) {
	// 在实际项目中，这里会添加Swagger文档路由
	// 例如：router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
